package com.ruoyi.business.controller;

import com.ruoyi.business.domain.CarPackageAudit;
import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.service.ICarPackageAuditService;
import com.ruoyi.business.service.ITodoTaskService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/business/todo")
public class TodoTaskController extends BaseController {
    private String prefix = "business/todo";
    @Autowired
    private ITodoTaskService todoTaskService;

    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    @GetMapping
    public String carPackageAudit() {
        return prefix+"/todoPage";
    }
    @GetMapping("/showAuditDialog/{taskId}")
    public String showAuditDialog(@PathVariable Long taskId, ModelMap modelMap) {
        modelMap.addAttribute("taskId",taskId);
        return prefix+"/auditDialog";
    }

    @GetMapping("/serviceItem/edit")
    public String serviceItemEdit(Long carPackageAuditId,ModelMap modelMap) {
        CarPackageAudit carPackageAudit = carPackageAuditService.selectCarPackageAuditById(carPackageAuditId);
        if(carPackageAudit==null) {
            throw new BusinessException("审核记录不存在");
        }
        //服务项对象
        modelMap.addAttribute("serviceItem",carPackageAudit.getServiceItem());
        //审核记录Id
        modelMap.addAttribute("carPackageAuditId",carPackageAuditId);

        return prefix+"/editServiceItem";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarPackageAudit carPackageAudit) {
        return todoTaskService.selectTodoList(carPackageAudit);
    }

    @Log(title = "审核任务", businessType = BusinessType.OTHER)
    @PostMapping("/complete/{taskId}")
    @ResponseBody
    public AjaxResult complete(@PathVariable String taskId,boolean auditStatus,String comment)
    {
        todoTaskService.taskAudit(taskId,auditStatus,comment);
        return success("审批完成！");
    }



    @Log(title = "重新申请", businessType = BusinessType.OTHER)
    @PostMapping("/reApply")
    @ResponseBody
    public AjaxResult reApply(Long carPackageAuditId,String taskId)
    {
        todoTaskService.reApply(carPackageAuditId,taskId);
        return success("申请成功！");
    }


    @Log(title = "修改服务", businessType = BusinessType.UPDATE)
    @PostMapping("/serviceItem/edit")
    @ResponseBody
    public AjaxResult editServiceItem(ServiceItem serviceItem,Long carPackageAuditId)
    {
        todoTaskService.updateServiceItem(serviceItem,carPackageAuditId);
        return success("修改完成！");
    }
}
